﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;

namespace BLL
{
  public static   class 来往明细BLL
    {
        public static void 增加来往记录(DanjuTable danju )
        {
            LwDetail lwDetail = new LwDetail();
            switch (danju.djlx )
            {
                case DanjuLeiXing.销售出库单:             
                     lwDetail = 销售单(danju);                  
                    break ;
                case DanjuLeiXing.色卡采购单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.色卡销售单 :
                    lwDetail = 销售单 (danju);
                    break;
                case DanjuLeiXing.采购入库单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.原料入库单 :
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.销售客诉单:
                    lwDetail = 销售退货单(danju);
                    break;
                case DanjuLeiXing.销售退货单 :
                    lwDetail = 销售退货单 (danju);
                    break;
                case DanjuLeiXing.采购退货单 :
                    lwDetail = 采购退货单 (danju);
                    break;
                case DanjuLeiXing.委外取货单 :
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.付款单 :
                    lwDetail = 付款单(danju);
                    break;
                case DanjuLeiXing.收款单:
                    lwDetail = 收款单 (danju);
                    break;
                case DanjuLeiXing.发票签收:
                    lwDetail = 发票签收(danju);
                    break;
                case DanjuLeiXing.发票开具 :
                    lwDetail = 发票开具 (danju);
                    break;
                case DanjuLeiXing.寄样单 :
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.费用单 :
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.打样单 :
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.销售订单 :
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.入库单 :
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.退卷单 :
                    lwDetail = 销售单(danju);
                    break;
            }
            Connect.dbcontext.Insertable(lwDetail ).ExecuteCommand ();
            新增刷新(danju);
        }
        #region "生成对应来往明细单据"
        private static LwDetail 费用单(DanjuTable danju )
        {
            var lw = new LwDetail()
            {
                AddYingFukuan = danju.je,
                DH = danju.dh,
                JE = danju.je,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,

            };
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = danju.je;
                lw.AddYingShouFapiao = danju.je;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
            lw.QichuJine = LXRService.GetOneLXR(x => x.BH == danju.ksbh).JE;
            lw.QiMojine = lw.QichuJine + danju.je;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
            return lw;
        }
        private static LwDetail 销售单(DanjuTable danju)
        {
            var lw = CreateNewIncreaseReceivablesLwDetail(danju);
            // var hanshuijine = Connect.dbcontext.Queryable<danjumingxitable>().Where (x=> x.danhao == danju.dh&&x.IsHanshui==IsHanshuiModel.含税 ).Sum (x=>x.hanshuiheji );
            var hanshuijine = Connect.dbcontext.Queryable<danjumingxitable>().Where (x=> x.danhao == danju.dh&&x.IsHanshui==IsHanshuiModel.含税 ).Sum (x=>x.hanshuiheji );
            GetQichujine(danju);
            if (danju.Hanshui=="含税")
            {
                lw.FapiaoJine = hanshuijine ;
                lw.AddYingKaiFapiao = hanshuijine;
                lw.AddYingshoukuan = danju.je;
                //lw.PeriodEndInvoices = lw.OpeningInvoices + lw.FapiaoJine;
            }
            else
            {
                lw.FapiaoJine = 0;
            }     
           GetQimo(lw,danju) ;
            return lw;
        }
        private static void GetQichujine(DanjuTable danju)
        {
            GetOpeningAmount(danju);
        }
        private static void GetQimo(LwDetail lw, DanjuTable danju)
        {
             GetQichujine(danju);
            lw.QiMojine = lw.QichuJine + lw.AddYingshoukuan -lw.ReduceYingShouKuan +lw.AddYingFukuan-lw.ReduceYingFuKuan  ;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui ;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
        }
        private static void GetQimo(LwDetail lw,DanjuTable danju,danjumingxitable mingxi)
        {
            GetQichujine(danju);
            lw.QiMojine = lw.QichuJine + mingxi.hanshuiheji;
            lw.own = danju.own;
            lw.Hanshui = mingxi.IsHanshui;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
        }
        /// <summary>
        /// 生成一个增加应收款的LWDETAIL
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="mingxi"></param>
        /// <returns></returns>
        private static LwDetail CreateNewIncreaseReceivablesLwDetail(DanjuTable danju, danjumingxitable mingxi)
        {
            return new LwDetail()
            {
                AddYingshoukuan = mingxi.hanshuiheji,
                DH = danju.dh,
                JE = mingxi.hanshuiheji,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
        }
        /// <summary>
        /// 生成一个增加应收款的LWDETAIL
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="mingxi"></param>
        /// <returns></returns>
        private static LwDetail CreateNewIncreaseReceivablesLwDetail(DanjuTable danju)
        {
            return new LwDetail()
            {
                AddYingshoukuan = danju.je ,
                DH = danju.dh,
                JE =danju.je ,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
        }
        /// <summary>
        /// 生成一个减少应收款的LWDETAIL
        /// </summary>
        /// <param name="danju"></param>
        /// <param name="mingxi"></param>
        /// <returns></returns>
        private static LwDetail CreateNewDecreaseReceivablesLwDetail(DanjuTable danju)
        {
            return new LwDetail()
            {
                ReduceYingShouKuan = danju.je,
                DH = danju.dh,
                JE = danju.je,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
        }
        private static LwDetail 销售单(DanjuTable danju,danjumingxitable mingxi)
        {
            var lw = CreateNewIncreaseReceivablesLwDetail( danju,  mingxi);
            IncreaseInvoice(lw, danju);
            GetQimo(lw, danju, mingxi);
            return lw;
        }
        /// <summary>
        /// 增加应收发票
        /// </summary>
        /// <param name="lw"></param>
        /// <param name="danju"></param>
        private static void IncreaseInvoice(LwDetail lw,DanjuTable danju )
        {
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = danju.je;
                lw.ReduceYingShouFapiao = danju.je;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
        }
        private static LwDetail 采购退货单(DanjuTable danju)
        {
            var lw = CreateNewIncreaseReceivablesLwDetail(danju);
            IncreaseInvoice(lw, danju);
            GetQimo(lw, danju);
            return new LwDetail ();
        }
        private static LwDetail 采购单(DanjuTable danju)
        {
            var lw = new LwDetail()
            {
                AddYingFukuan  = danju.je,
                DH = danju.dh,
                JE = danju.je,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
                
            };
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = danju.je;
                lw.AddYingShouFapiao = danju.je;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
            lw.QichuJine = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == danju.ksbh).JE;
            lw.QiMojine = lw.QichuJine + danju.je;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
            return lw;
        }
        private static LwDetail 销售退货单(DanjuTable danju)
        {
            var lw = CreateNewDecreaseReceivablesLwDetail(danju);
            // var hanshuijine = Connect.dbcontext.Queryable<danjumingxitable>().Where (x=> x.danhao == danju.dh&&x.IsHanshui==IsHanshuiModel.含税 ).Sum (x=>x.hanshuiheji );
            var hanshuijine = danju.je;
            GetQichujine(danju);
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = hanshuijine;
                lw.ReduceYingKaiFapiao = hanshuijine;
                //lw.PeriodEndInvoices = lw.OpeningInvoices + lw.FapiaoJine;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
            GetQimo(lw, danju);
            return lw;
        }
        private static LwDetail 付款单(DanjuTable danju)
        {
            var lw = new LwDetail()
            {
                ReduceYingFuKuan = danju.je +danju.totalmoney ,
                DH = danju.dh,
                JE = danju.je,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = danju.je;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
            lw.QichuJine = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == danju.ksbh).JE;
            lw.QiMojine = lw.QichuJine- danju.je;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;
            return lw;
        }
        private static LwDetail 收款单(DanjuTable danju)
        {
            var lw = new LwDetail()
            {
                ReduceYingShouKuan = danju.je+danju.totalmoney ,
                DH = danju.dh,
                JE = danju.je,
                bz = danju.bz,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
            if (danju.Hanshui == "含税")
            {
                lw.FapiaoJine = danju.je;
            }
            else
            {
                lw.FapiaoJine = 0;
            }
            lw.QichuJine = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == danju.ksbh).JE;
            lw.QiMojine = lw.QichuJine - danju.je;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;        
            lw.debt = false;
            return lw;
        }
        private static LwDetail 发票签收(DanjuTable danju)
        {
            var lw = new LwDetail()
            {
                ReduceYingShouFapiao = danju.je,
                DH = danju.dh,
                bz = danju.bz,
                FapiaoJine = danju.je,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
            lw.QichuJine = Connect.dbcontext.Queryable<LXR>().First(x => x.BH == danju.ksbh).JE;
            lw.QiMojine = lw.QichuJine ;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
            return lw;
        }
        private static LwDetail 发票开具(DanjuTable danju)
        {
            var lw = new LwDetail()
            {
                ReduceYingKaiFapiao = danju.je,
                DH = danju.dh,
                bz = danju.bz,
                FapiaoJine = danju.je,
                KHBH = danju.ksbh,
                KHMC = danju.ksmc,
                LX = danju.djlx,
                rq = danju.rq,
            };
            GetQichujine(danju);
            lw.QiMojine = lw.QichuJine;
            //lw.PeriodEndInvoices = lw.OpeningInvoices - lw.ReduceYingKaiFapiao;
            lw.own = danju.own;
            lw.Hanshui = danju.Hanshui;
            if (danju.Qiankuan == "欠款")
            {
                lw.debt = true;
            }
            else
            {
                lw.debt = false;
            }
            return lw;
        }
        #endregion 
        public static void 删除来往记录(DanjuTable danju)
        {           
            Connect.dbcontext.Deleteable<LwDetail>().Where (x => x.DH == danju.dh).ExecuteCommand();
            新增刷新(danju);
        }
        //private static void 刷新(DanjuTable Olddanju )
        //{
        //        DanjuTable danju = Olddanju;         
        //        var dt = Connect.dbcontext.Ado.GetDataTable ($"select * from lwdetail where dh='{danju.dh}' ");
        //        int id = 0;
        //        if (dt.Rows.Count > 0)
        //        {
        //            id = (int)dt.Rows[0]["ID"];
        //        }
        //        var lwlist = Connect.dbcontext.Ado.SqlQuery<LwDetail>($"select * from lwdetail where rq>='{danju.rq }' and khbh='{danju.ksbh }'  order by rq asc , id asc");
        //        if (lwlist.Count > 1)
        //        {
        //            decimal qichu, qimo;
        //            qichu = lwlist[0].QichuJine;
        //            qimo = lwlist[0].QiMojine;
        //            foreach (var lw in lwlist)
        //            {
        //                if (lw.DH != danju.dh)
        //                {
        //                    if (lw.LX != DanjuLeiXing.发票开具 && lw.LX != DanjuLeiXing.发票签收&&lw.LX!=DanjuLeiXing.退卷单 &&lw.LX!=DanjuLeiXing.入库单)
        //                    {
        //                        lw.QichuJine = qichu;
        //                        lw.QiMojine = lw.QichuJine + lw.AddYingFukuan + lw.AddYingshoukuan - lw.ReduceYingShouKuan - lw.ReduceYingFuKuan;
        //                       Connect.dbcontext.Updateable(lw).ExecuteCommand ();
        //                        qichu = lw.QiMojine;
        //                    }
        //                }
        //            }
        //        }
        //}
        private static void 新增刷新(DanjuTable danju)
        {
            //$"select * from lwdetail where rq>='{danju.rq }' and khbh='{danju.ksbh}'  order by rq asc"
            var lwlist = Connect.dbcontext.Queryable<LwDetail>().Where(x=>x.rq>=danju.rq &&x.KHBH==danju.ksbh ).OrderBy(x=>x.rq).ToList ();
            if (lwlist.Count >=1)
            {
                decimal qichu;                              
                qichu =GetOpeningAmount(danju);          
                foreach (var lw in lwlist)
                {                   
                        if (lw.LX != DanjuLeiXing.发票开具 && lw.LX != DanjuLeiXing.发票签收 && lw.LX != DanjuLeiXing.退卷单 && lw.LX != DanjuLeiXing.入库单)
                        {
                            lw.QichuJine = qichu;
                            lw.QiMojine = lw.QichuJine + lw.AddYingFukuan + lw.AddYingshoukuan - lw.ReduceYingShouKuan - lw.ReduceYingFuKuan;
                       
                         qichu = lw.QiMojine;
                        }
                }
            }
            Connect.dbcontext.Updateable(lwlist).ExecuteCommand();
        }
        public static void 修改(DanjuTable danju )
        {
            LwDetail lwDetail = new LwDetail();
            switch (danju.djlx)
            {
                case DanjuLeiXing.销售出库单:
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.色卡采购单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.色卡销售单:
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.采购入库单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.销售退货单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.采购退货单:
                    lwDetail = 销售单(danju);
                    break;
                case DanjuLeiXing.委外取货单:
                    lwDetail = 采购单(danju);
                    break;
                case DanjuLeiXing.付款单:
                    lwDetail = 付款单(danju);
                    break;
                case DanjuLeiXing.收款单:
                    lwDetail = 收款单(danju);
                    break;
                case DanjuLeiXing.发票签收:
                    lwDetail = 发票签收(danju);
                    break;
                case DanjuLeiXing.发票开具:
                    lwDetail = 发票开具(danju);
                    break;
                case DanjuLeiXing.寄样单 :
                    lwDetail = 销售单(danju);
                    break;
            }
            Connect.dbcontext.Updateable(lwDetail).ExecuteCommand();
            新增刷新(danju );
        }
        /// <summary>
        /// 获取期初
        /// </summary>
        /// <param name="danju"></param>
        /// <returns></returns>
        private static decimal GetOpeningAmount(DanjuTable danju)
        {
            var lws = Connect.dbcontext.Ado.SqlQuery<LwDetail>($"select top(1)* from LwDetail where KHBH='{danju.ksbh}' and rq<='{danju.rq}' order by rq desc");
            return lws .Count==0 ? 0: lws[0].QiMojine;

        }
    }
}
